package com.atguigu.gulimall.search.thread;

import java.util.concurrent.*;

/**
 * 异步测试类
 *
 * @author LaiSanShan
 * @date 2021-08-16
 */
public class ThreadTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new Thread01().start();
        new Thread(new Thread02()).start();
        new Thread(new FutureTask<>(new Thread03())).start();
        thread04();
    }

    /**
     * 继承Thread
     */
    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("继承Thread：子线程执行。。。" + Thread.currentThread().getId());
        }
    }

    /**
     * 实现Runnable
     */
    public static class Thread02 implements Runnable {
        @Override
        public void run() {
            System.out.println("实现Runnable：子线程执行。。。" + Thread.currentThread().getId());
        }
    }

    /**
     * 实现Callable
     */
    public static class Thread03 implements Callable<Integer> {
        @Override
        public Integer call() {
            System.out.println("实现Callable：子线程执行。。。" + Thread.currentThread().getId());
            return 1;
        }
    }

    /**
     * 用线程池，把任务提交给它运行
     */
    private static void thread04() throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        threadPool.execute(new Thread02());
        Future<Integer> submit = threadPool.submit(new Thread03());
        Integer res = submit.get();
        System.out.println(res);
    }

}
